<template>
  <div class="api-test-container">
    <h2>API路径测试</h2>
    
    <div class="test-section">
      <h3>当前配置</h3>
      <p><strong>API基础路径:</strong> {{ apiBase }}</p>
      <p><strong>后端服务:</strong> http://localhost:7003</p>
    </div>

    <div class="test-section">
      <h3>测试API连接</h3>
      <div class="test-buttons">
        <button @click="testPurchaseOrder" class="btn btn-primary">
          测试采购订单API
        </button>
        <button @click="testPurchaseReturn" class="btn btn-success">
          测试采购退货单API
        </button>
        <button @click="testPurchaseReturnAudit" class="btn btn-info">
          测试审核API
        </button>
        <button @click="testAll" class="btn btn-secondary">
          测试所有API
        </button>
      </div>
    </div>

    <div class="test-section">
      <h3>测试结果</h3>
      <div v-for="(result, index) in testResults" :key="index" class="test-result">
        <div class="result-header">
          <span class="result-name">{{ result.name }}</span>
          <span class="result-status" :class="result.success ? 'success' : 'error'">
            {{ result.success ? '✅ 成功' : '❌ 失败' }}
          </span>
        </div>
        <div class="result-details">
          <p><strong>URL:</strong> {{ result.url }}</p>
          <p><strong>状态:</strong> {{ result.status }}</p>
          <p v-if="result.error"><strong>错误:</strong> {{ result.error }}</p>
          <p v-if="result.response"><strong>响应:</strong> {{ result.response }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ApiTest',
  data() {
    return {
      apiBase: '',
      testResults: []
    }
  },
  mounted() {
    // 获取当前API基础路径
    this.apiBase = window.location.origin
  },
  methods: {
    async testPurchaseOrder() {
      await this.testApi('采购订单API', '/purchase-order/page?current=1&size=10')
    },
    
    async testPurchaseReturn() {
      await this.testApi('采购退货单API', '/purchase-return-order/page?current=1&size=10')
    },
    
    async testPurchaseReturnAudit() {
      await this.testApi('审核API', '/purchase-return-order-audit/record/1')
    },
    
    async testAll() {
      this.testResults = []
      await this.testPurchaseOrder()
      await this.testPurchaseReturn()
      await this.testPurchaseReturnAudit()
    },
    
    async testApi(name, path) {
      const url = `${this.apiBase}${path}`
      const result = {
        name,
        url,
        success: false,
        status: '',
        error: '',
        response: ''
      }
      
      try {
        console.log(`测试 ${name}: ${url}`)
        const response = await fetch(url)
        result.status = `${response.status} ${response.statusText}`
        
        if (response.ok) {
          result.success = true
          try {
            const data = await response.json()
            result.response = JSON.stringify(data, null, 2)
          } catch (e) {
            result.response = '响应不是JSON格式'
          }
        } else {
          result.error = `HTTP ${response.status}: ${response.statusText}`
        }
      } catch (error) {
        result.error = error.message
        console.error(`测试 ${name} 失败:`, error)
      }
      
      this.testResults.push(result)
    }
  }
}
</script>

<style scoped>
.api-test-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.test-section {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #f9f9f9;
}

.test-section h3 {
  margin-top: 0;
  color: #333;
}

.test-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.btn-primary {
  background: #007bff;
  color: white;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-info {
  background: #17a2b8;
  color: white;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn:hover {
  opacity: 0.8;
}

.test-result {
  margin-bottom: 15px;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.result-name {
  font-weight: bold;
  color: #333;
}

.result-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.result-status.success {
  background: #d4edda;
  color: #155724;
}

.result-status.error {
  background: #f8d7da;
  color: #721c24;
}

.result-details p {
  margin: 5px 0;
  font-size: 14px;
}

.result-details strong {
  color: #555;
}
</style> 